package com.example.testgit.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.testgit.VO.*;
import com.example.testgit.entity.AwardDO;
import com.example.testgit.entity.LoanDO;
import com.example.testgit.entity.PunishmentDO;
import com.example.testgit.entity.WorkStudyDO;
import com.example.testgit.mapper.AwardMapper;
import com.example.testgit.mapper.LoanMapper;
import com.example.testgit.mapper.PunishmentMapper;
import com.example.testgit.mapper.WorkStudyMapper;
import com.example.testgit.mapstruct.AwardConverter;
import com.example.testgit.mapstruct.LoanConverter;
import com.example.testgit.mapstruct.PunishmentConverter;
import com.example.testgit.service.AwardPunishmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author: lyh
 * @create: 2023/5/8 19:56
 * Description：
 */
@Service
public class AwardPunishmentServiceImpl implements AwardPunishmentService {

    @Resource
    private AwardMapper awardMapper;

    @Resource
    private LoanMapper loanMapper;

    @Resource
    private WorkStudyMapper workStudyMapper;

    @Resource
    private PunishmentMapper punishmentMapper;

    @Override
    public PageResult<AwardVO> queryAward(StudentQuery stu) {
        QueryWrapper<AwardDO> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id", stu.getStuId());
        wrapper.eq("award_type", stu.getType());
        Page<AwardDO> page = new Page<>(stu.getPageNum(), stu.getPageSize());

        Page<AwardDO> awardDOPage = awardMapper.selectPage(page, wrapper);
        PageResult pageResult = new PageResult(awardDOPage);
        pageResult.setData(AwardConverter.INSTANCE.toVOList(awardDOPage.getRecords()));

        // 获奖次数以及总金额计算
        List<AwardDO> awardDOS = awardMapper.selectList(wrapper);
        int num = 0;
        int sum = 0;
        for (AwardDO res : awardDOS) {
            num++;
            sum += res.getAwardAmount();
        }
        JSONObject json = new JSONObject();
        json.put("num", num);
        json.put("sum", sum);
        pageResult.setNote(json);

        return pageResult;
    }

    @Override
    public PageResult<LoanVO> queryLoan(StudentQuery stu) {
        QueryWrapper<LoanDO> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id", stu.getStuId());
        Page<LoanDO> page = new Page<>(stu.getPageNum(), stu.getPageSize());

        Page<LoanDO> loanDOPage = loanMapper.selectPage(page, wrapper);
        PageResult pageResult = new PageResult(loanDOPage);
        pageResult.setData(LoanConverter.INSTANCE.toVOList(loanDOPage.getRecords()));

        // 计算贷款金额以及还款金额
        List<LoanDO> loanDOS = loanMapper.selectList(wrapper);
        int loan = 0;
        int retuened = 0;
        for (LoanDO res : loanDOS) {
            loan += res.getMount();
            retuened += res.getReturned();
        }
        JSONObject json = new JSONObject();
        json.put("loan", loan);
        json.put("retuened", retuened);
        pageResult.setNote(json);

        return pageResult;
    }

    @Override
    public WorkStudyVO queryWorkStudy(Integer stuId) {
        QueryWrapper<WorkStudyDO> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id", stuId);

        List<WorkStudyDO> workStudyDOList = workStudyMapper.selectList(wrapper);
        WorkStudyVO res = new WorkStudyVO();

        // 返回结果计算
        List<WorkStudyDO> workStudyDOS = workStudyMapper.selectList(wrapper);
        int currrentYear = new Date().getYear() + 1900;
        int totalHours = 0;
        int stat[] = new int[12];
        for (WorkStudyDO resDO : workStudyDOS){
            totalHours += resDO.getHours();

            if (currrentYear == resDO.getParticipationTime().getYear() + 1900) {
                int month = resDO.getParticipationTime().getMonth();
                stat[month] += resDO.getHours();
            }
        }
        res.setStuId(stuId);
        res.setNum(workStudyDOList.size());
        res.setTotalHours(totalHours);
        res.setStat(stat);
        return res;
    }

    @Override
    public PageResult<PunishmentVO> queryPunishment(StudentQuery stu) {
        QueryWrapper<PunishmentDO> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id", stu.getStuId());
        Page<PunishmentDO> page = new Page<>(stu.getPageNum(), stu.getPageSize());

        Page<PunishmentDO> punishmentDOPage = punishmentMapper.selectPage(page, wrapper);
        PageResult pageResult = new PageResult(punishmentDOPage);
        pageResult.setData(PunishmentConverter.INSTANCE.toVOList(punishmentDOPage.getRecords()));

        // 惩处信息统计
        List<PunishmentDO> punishmentDOS = punishmentMapper.selectList(wrapper);
        int warn = 0;
        int demerit = 0;
        int serious_warning = 0;
        int detention = 0;
        for (PunishmentDO res : punishmentDOS) {
            if (res.getPunishType() == 0) warn++;
            else if (res.getPunishType() == 1) demerit++;
            else if (res.getPunishType() == 2) serious_warning++;
            else detention++;
        }
        JSONObject json = new JSONObject();
        json.put("warn", warn);
        json.put("demerit", demerit);
        json.put("serious_warning", serious_warning);
        json.put("detention", detention);
        pageResult.setNote(json);

        return pageResult;
    }
}
